package leetcode;

import java.util.HashMap;
import java.util.Set;

public class LongestConcursiveSequence {

	public static void main(String[] args) {
		LongestConcursiveSequence lcs = new LongestConcursiveSequence();
		int[] nums = {4,0,-4,-2,2,5,2,0,-8,-8,-8,-8,-1,7,4,5,5,-4,6,6,-3};
		System.out.println(lcs.longestConsecutive(nums));
	}
	public int longestConsecutive(int[] nums) {
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		for (int i = 0; i < nums.length; i++) {
			map.put(nums[i], 1);
		}
		Set<Integer> set = map.keySet();
		System.out.println(set);
		for (Integer num : set) {
			if (map.get(num - 1) != null) {
				System.out.println("num : " + num);
				map.put(num, get(map, num - 1) + 1);
			}
		}
		for (Integer integer : set) {
			System.out.println(integer + "after change: " + map.get(integer));
		}
		int max = 0;
		Integer temp = 0;
		for (Integer num : set) {
			if ((temp = map.get(num)) > max) {
				max = temp;
			}
		}
		return max;
	}

	public int get(HashMap<Integer, Integer> map, int num) {
		Integer res = 0;
		// 大于1表示已经计算过了
		if ((res = map.get(num)) > 1) {
			return res;
		} else if ((res = map.get(num - 1)) != null) {
			// 如果下一个相邻的数存在
			return get(map, num - 1) + 1;
		} else {
			// 如果不存在，那么直接返回即可
			return map.get(num);
		}
	}
}
